package com.example.framework;

import org.openqa.selenium.By;

import com.example.framework.dataobjects.ContactObject;


public class ContactHelper extends FormHelper{



	public ContactHelper(ApplicationManager manager){
		super(manager);

	}
	
	public void createContact(ContactObject contactCredentials) {
		manager.getNavigationHelper().openContactForm();
		populateNewContactForm(contactCredentials);
		submitCreateContactForm();
	}

	

	private void submitCreateContactForm() {
		clickOnButton("submit");
	}
	
	public void createNewContact(ContactObject contactCredentials) {
		manager.getContactHelper().createContact(contactCredentials);
		manager.getNavigationHelper().goToHomePage();
	}
	

	protected void populateNewContactForm(ContactObject contactValues) {
		//populate new contact form
		String firstNameLocator = "firstname";
		String lastNameLocator = "lastname";
		String addressLocator = "address";
		String homeLocator = "home";
		String mobileLocator = "mobile";
		String workLocator = "work";
		
		String emailFieldLocator = "email";
		String email2FieldLocator = "email2";
		
		String secondaryAddressFieldLocator = "address2";
		String secondaryPhoneFieldLocator = "phone2";
		
	
		type(By.name(firstNameLocator), contactValues.getFirstName());
		type(By.name(lastNameLocator), contactValues.getLastName() );
		type(By.name(addressLocator), contactValues.getAddress() );
		type(By.name(homeLocator), contactValues.getHome());
		type(By.name(mobileLocator), contactValues.getMobile());
		type(By.name(workLocator), contactValues.getWork());
		type(By.name(emailFieldLocator), contactValues.getEmail());
		type(By.name(email2FieldLocator), contactValues.getEmail2());
		
		selectContactBirthday(contactValues.getbDay(),
				contactValues.getbMonth(), contactValues.getbYear());
		
		selectContactGroup(contactValues.getGroup());

		type(By.name(secondaryAddressFieldLocator), contactValues.getSecondaryAddress() );
		type(By.name(secondaryPhoneFieldLocator), contactValues.getSecondaryPhone());
	}
	
	protected void populateEditContactForm(ContactObject contactValues) {
		//populate new contact form
		String firstNameLocator = "firstname";
		String lastNameLocator = "lastname";
		String addressLocator = "address";
		String homeLocator = "home";
		String mobileLocator = "mobile";
		String workLocator = "work";
		
		String emailFieldLocator = "email";
		String email2FieldLocator = "email2";
		
		String secondaryAddressFieldLocator = "address2";
		String secondaryPhoneFieldLocator = "phone2";
		
	
		type(By.name(firstNameLocator), contactValues.getFirstName());
		type(By.name(lastNameLocator), contactValues.getLastName() );
		type(By.name(addressLocator), contactValues.getAddress() );
		type(By.name(homeLocator), contactValues.getHome());
		type(By.name(mobileLocator), contactValues.getMobile());
		type(By.name(workLocator), contactValues.getWork());
		type(By.name(emailFieldLocator), contactValues.getEmail());
		type(By.name(email2FieldLocator), contactValues.getEmail2());
		
		selectContactBirthday(contactValues.getbDay(),
				contactValues.getbMonth(), contactValues.getbYear());

		
		type(By.name(secondaryAddressFieldLocator), contactValues.getSecondaryAddress() );
		type(By.name(secondaryPhoneFieldLocator), contactValues.getSecondaryPhone());
	}
	

	protected void selectContactGroup(String groupName) {
		selectItemFromDropDownList(By.name("new_group"), groupName);
	}

	protected void selectContactBirthday(
			String day, String month,
			String year) {
		
		selectItemFromDropDownList(By.name("bday"), day);
		selectItemFromDropDownList(By.name("bmonth"), month);
		type(By.name("byear"), year);
	}

	public void modifyContact(int i, ContactObject contactCredentials) {
		manager.getNavigationHelper().goToContactListPage();
		initContactModification(i, contactCredentials);
		manager.getNavigationHelper().goToHomePage();
	}

	private void submitContactModification() {
		clickOnButton("update");	
	}

	public void checkContact(int i) {
		click(By.id("id" + i));
	}

	private void initContactModification(int i, ContactObject contactCredentials) {
		//manager.getNavigationHelper().openEditContactFormPage(i);	
		modifyFirstRecordInTable();
		populateEditContactForm(contactCredentials);
		submitContactModification();
	}

	public void deleteContact() {
		manager.getNavigationHelper().goToContactListPage();
		modifyFirstRecordInTable();
		clickOnButtonDelete();
		manager.getNavigationHelper().goToHomePage();
	}

	private void modifyFirstRecordInTable() {
		click(By.cssSelector("img[alt=\"Edit\"]"));

	}

	private void clickOnButtonDelete() {
		click(By.xpath("(//input[@name='update'])[2]"));

	}
	
	
	

}
